package com.gohome.gohomechat.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.Friend;
import com.gohome.gohomechat.presenter.AddFriendPresenter;
import com.gohome.gohomechat.presenter.presenterlistener.AddFriendPresenterInterface;
import com.gohome.gohomechat.ui.adapter.AddFriendListViewAdapter;
import com.gohome.gohomechat.ui.viewlistener.AddFriendViewInterface;
import com.gohome.gohomechat.utils.ToastUtil;

import java.util.List;

/**
 * Created by dllo on 16/3/2.
 */
public class AddFriendActivity extends BaseActivity implements View.OnClickListener, AddFriendViewInterface, AddFriendListViewAdapter.onClickButtonListener {
    private ImageButton addfriend_imgbtn_back;
    private TextView addfriend_text_find;
    private ListView listView;
    private AddFriendListViewAdapter adapter;
    private EditText addfriend_et_find, reason;
    private AddFriendPresenterInterface addFriendPresenterInterface;
    private String friendName="";

    @Override
    protected int getLayout() {
        return R.layout.activity_addfriend;
    }

    @Override
    protected void initPresenter() {
        addFriendPresenterInterface=new AddFriendPresenter(this);
    }

    @Override
    protected void initView() {

        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            friendName=bundle.getString("friend");
            Log.i("wer","穿名字 "+friendName);
        }
        addfriend_imgbtn_back = findView(R.id.addfriend_imgbtn_back);
        addfriend_text_find = findView(R.id.addfriend_text_find);
        listView = findView(R.id.addfriend_listview);
        adapter = new AddFriendListViewAdapter(this);
        addfriend_et_find = findView(R.id.addfriend_et_find);
    }

    @Override
    protected void initData() {
        listView.setAdapter(adapter);
        addfriend_imgbtn_back.setOnClickListener(this);
        addfriend_text_find.setOnClickListener(this);
        adapter.setOnClickButtonListener(this);

        if(!friendName.equals("")){
            addfriend_et_find.setText(friendName);
            addFriendPresenterInterface.showAlertDialog();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.addfriend_imgbtn_back:
                finish();
                break;
            case R.id.addfriend_text_find:
                addFriendPresenterInterface.refreshData();
                break;
        }
    }

    @Override
    public void showDialog() {
        showProgressDialog();
    }

    @Override
    public void dismissDialog() {
        dismissProgressDialog();
    }

    @Override
    public String userName() {
        return addfriend_et_find.getText().toString().trim();
    }

    @Override
    public String reason() {
        if (reason == null || "".equals(reason.getText().toString().toString()))
            return "加个好友呗,亲!";
        else
            return reason.getText().toString().toString();
    }



    @Override
    public void showAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("添加理由:");
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_edittext, null);
        reason = (EditText) view.findViewById(R.id.dialog_et_reason);
        builder.setView(view);
        builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                addFriendPresenterInterface.addFriend();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.show();
    }

    @Override
    public void addFriendSuccess() {
        ToastUtil.showToast("请求发送成功,等待对方验证!");
    }

    @Override
    public void addFriendFailed() {
        ToastUtil.showToast("请求失败!");
    }

    @Override
    public void updateData(List<Friend> datas) {
        adapter.addData(datas);
    }

    @Override
    public void updateDataFailed() {
        ToastUtil.showToast("没有查找到该好友!");
    }

    @Override
    public void addFriend() {
        addFriendPresenterInterface.showAlertDialog();
    }
}
